#!/bin/bash

# Copyright 2017-2024 NXP
#
# SPDX-License-Identifier: BSD-3-Clause


build_distro_rfs_debian() {
    [ $DISTROTYPE = debian -a -f $RFSDIR/etc/buildinfo ] && echo $RFSDIR already exists! && exit 0

    echo generating $RFSDIR ... && cd $FBDIR
    test -c $RFSDIR/dev/pts/ptmx && sudo umount $RFSDIR/dev/pts
    test -f $RFSDIR/proc/uptime && sudo umount $RFSDIR/proc
    [ -n "$RFSDIR" ] && sudo rm -rf $RFSDIR/*

    if [ "$FETCH_PREBUILT_SDK_RFS" = y -a "$FORCE" != y ]; then
	echo FETCH_PREBUILT_SDK_RFS is enable by default in $CFGLISTYML
	rootfsn=rootfs_${DISTRIB_VERSION}_${DISTROTYPE}_${DISTROVARIANT}_${DESTARCH}.tar.zst
	rootfs_url=$DISTRO_SVR_URL/$DISTRIB_VERSION/$rootfsn
	if [ ! -f $FBOUTDIR/images/$rootfsn ]; then
	   cd $FBOUTDIR/images
	   wget --progress=bar:force $rootfs_url || true
	   cd -
	fi
	[ -f $FBOUTDIR/images/$rootfsn ] && echo Extracting $rootfsn && \
	sudo tar -I zstd -xf $FBOUTDIR/images/$rootfsn -C $RFSDIR && \
	fbprint_d $RFSDIR && exit || echo Building $RFSDIR from scratch
    fi

    if [ ! -f $PKGDIR/${DEBIAN_CODENAME}_${DISTROVARIANT}_$DESTARCH/rootfs/etc/buildinfo ]; then
	[ -f /usr/bin/bdebstrap ] || sudo apt-get install -y bdebstrap
	if grep -q Debian /etc/issue; then mirroropt="--mirrors https://deb.debian.org/debian"; fi
	[ -f $PKGDIR/${DEBIAN_CODENAME}_${DISTROVARIANT}_$DESTARCH ] || \
	sudo bdebstrap -c $FBDIR/configs/debian/${DISTROTYPE}_${DISTROVARIANT}_${DESTARCH}.yaml --name \
	     $PKGDIR/${DEBIAN_CODENAME}_${DISTROVARIANT}_$DESTARCH $mirroropt -f
    fi

    [ -f $RFSDIR/etc/buildinfo ] || sudo rsync -aHAX $PKGDIR/${DEBIAN_CODENAME}_${DISTROVARIANT}_$DESTARCH/rootfs/* $RFSDIR

    if [ $DISTROVARIANT = server -o $DISTROVARIANT = desktop ]; then
	sudo chroot $RFSDIR apt-get update
	[ $DISTROVARIANT = server ] && sudo chroot $RFSDIR apt-get install -y $extra_server_devel_packages
	[ $DISTROVARIANT = desktop ] && sudo chroot $RFSDIR apt-get install -y $extra_server_devel_packages $extra_desktop_devel_packages

	# set U-Boot version info in userland to be readable in linux runtime
	[ -d $FBOUTDIR/bsp/u-boot ] && uvfile=$(find $FBOUTDIR/bsp/u-boot/*/output/*/include/generated -name version_autogenerated.h | head -1)
	if [ -n "$uvfile" -a -f $uvfile ]; then
	    ubootVersion=`head -1 $uvfile | cut -d' ' -f3`
	    ubootVersion=`echo $ubootVersion | sed 's/\"//g'`
	    echo -e "Bootloader Version: U-Boot $ubootVersion" | sudo tee -a $RFSDIR/etc/buildinfo 1>/dev/null
	fi

	# clean cached packages
	sudo chroot $RFSDIR apt clean
	sudo chroot $RFSDIR apt autoclean
	sudo chroot $RFSDIR apt autoremove -y
    fi

    # fix absolute path for libz.so symlink when cross compiling apitrace
    sudo ln -sf libz.so.1 $RFSDIR/usr/lib/aarch64-linux-gnu/libz.so

    # fixup for libtinfo.so needed by tsntool
    sudo ln -sf libtinfo.so.6 $RFSDIR/usr/lib/aarch64-linux-gnu/libtinfo.so

    echo -e 'export PYTHONPATH=$PYTHONPATH:/usr/lib/python3.11/site-packages:/usr/lib/python3/dist-packages' | sudo tee -a $RFSDIR/etc/profile 1>/dev/null

    # fix ping: socket: Operation not permitted
    sudo chmod u+s $RFSDIR/bin/ping

    fbprint_d $RFSDIR
}
